package auctions;

import java.io.File;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.*;

public class AuctionsToXML {

	private Document _doc;
	private Vector<AuctionResults> _results;
	private Vector<BidderResults> _bresults;
	//private Vector<Agent> _agents;
	
	public AuctionsToXML(Vector<AuctionResults> results, Vector<BidderResults> bresults) {
		_results = results;
		_bresults = bresults;
		
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder parser = factory.newDocumentBuilder();
			Document doc = parser.newDocument();
			_doc = doc;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
	public void setResults(Vector<AuctionResults> results, Vector<BidderResults> bresults) {
		_results = results;
		_bresults = bresults;
	}
	
	public void generateXML(String filename) {
		//Print the all results to an XML file
		Element root = _doc.createElement("Results");
		_doc.appendChild(root);
		Element auctionResults = _doc.createElement("AuctionResults");
		root.appendChild(auctionResults);
		for (int i = 0; i< _results.size(); i++) {
			Element auction = _doc.createElement("Auction");
			auction.setAttribute("number", Integer.toString(i));
			auction.setAttribute("winningBidder", Integer.toString(_results.get(i).getWinningBidder()));
			auction.setAttribute("winningPrice", Double.toString(_results.get(i).getWinningPrice()));
			auctionResults.appendChild(auction);
			
			//Element winningBidder = _doc.createElement("Winning Bidder: " + );
			//Element winningPrice = _doc.createElement("Winning Price: " + );
			Element bids = _doc.createElement("Bids");
			Vector<Double> bidValues = _results.get(i).getBids();
			for (int j = 0; j<bidValues.size(); j++) {
				Element bid = _doc.createElement("Bid");
				bid.setAttribute("value", Double.toString(bidValues.get(j)));
				bid.setAttribute("Bidder", Integer.toString(j));
				bids.appendChild(bid);
			}
			auction.appendChild(bids);
			
			
		}
		
		Element bidderResults = _doc.createElement("BidderResults");
		root.appendChild(bidderResults);
		
		for (int i = 0; i< _bresults.size(); i++) {
			BidderResults br = _bresults.get(i);
			Element bidder = _doc.createElement("Bidder");
			bidder.setAttribute("Bidder", Integer.toString(i));
			bidder.setAttribute("Revenue", Double.toString(br.getRevenue()));
			bidder.setAttribute("Cost", Double.toString(br.getCost()));
			bidder.setAttribute("Profit", Double.toString(br.getProfit()));
			bidderResults.appendChild(bidder);
			Vector<Integer> iw = br.getItemsWon();
			for (int j = 0; j<iw.size(); j++) {
				Element item = _doc.createElement("Item");
				item.setAttribute("ItemNumber", Integer.toString(iw.get(j)));
				bidder.appendChild(item);
			}
		}
		
		this.writeXmlFile(_doc, filename);
		
	}
	
	private void writeXmlFile(Document doc, String filename) {
	    try {
	        // Prepare the DOM document for writing
	        Source source = new DOMSource(doc);

	        // Prepare the output file
	        File file = new File(filename);
	        Result result = new StreamResult(file);

	        // Write the DOM document to the file
	        Transformer xformer = TransformerFactory.newInstance().newTransformer();
	        xformer.transform(source, result);
	    } catch (Exception e) {
	    	System.out.println(e.getMessage());
	    }
	}

}
